﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using TaskLib.Scheduling;

namespace ConsoleTester
{
    class Program
    {
        static void Main(string[] args)
        {
            TaskSchedulerForPriority tsfp = new TaskSchedulerForPriority();
            List<Task> tasks = new List<Task>();

            TaskFactory factory = new TaskFactory(tsfp);
            CancellationTokenSource cts = new CancellationTokenSource();

            Object lockObj = new object();
            int outputItem = 0;


            for (int tCtr = 0; tCtr <= 1000; tCtr++)
            {
                int iteration = tCtr;
                Task t = factory.StartNew(() =>
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        lock (lockObj)
                        {
                            Console.Write("{0} in task t-{1} on thread {2}   ",
                                i, iteration, Thread.CurrentThread.ManagedThreadId);
                            outputItem++;
                            if (outputItem % 3 == 0)
                            {
                                Console.WriteLine();
                            }
                        }
                    }
                }, cts.Token);
                tasks.Add(t);
            }

            for (int tCtr = 0; tCtr <= 1000; tCtr++)
            {
                int iteration = tCtr;
                Task t1 = factory.StartNew(() =>
                {
                    for (int outer = 0; outer <= 10; outer++)
                    {
                        for (int i = 0x21; i <= 0x7e; i++)
                        {
                            lock (lockObj)
                            {
                                Console.Write("'{0}' in task t1-{1} on thread {2}   ",
                                    Convert.ToChar(i), iteration, Thread.CurrentThread.ManagedThreadId);
                                outputItem++;
                                if (outputItem % 3 == 0)
                                {
                                    Console.WriteLine();
                                }
                            }
                        }
                    }
                }, cts.Token);
                tasks.Add(t1);
            }


            for (int tCtr = 0; tCtr <= 200; tCtr++)
            {
                int iteration = tCtr;
                CustomTask t = new CustomTask(() =>
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        lock (lockObj)
                        {
                            Console.Write("{0} in task t-{1} on thread {2}   ",
                                i, iteration, Thread.CurrentThread.ManagedThreadId);
                            outputItem++;
                            if (outputItem % 3 == 0)
                            {
                                Console.WriteLine();
                            }
                        }
                    }
                }, cts.Token)
                {
                    Priority = TaskPriority.Urgent
                };
                t.RunSynchronously(tsfp);
                tasks.Add(t);
            }


            Task.WaitAll(tasks.ToArray());
            cts.Dispose();
            Console.WriteLine("\n\nSuccessful completion.");
        }
    }
}
